﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Media3D;

namespace Eniux.Windows.ThreeDEngine.ExtensionMethods
{
    /// <summary>
    /// Vector3D的扩展方法
    /// </summary>
    public static class Vector3DExtensions
    {
        #region 公共方法

        /// <summary>
        /// 查找给定三维向量的垂直向量
        /// </summary>
        /// <param name="n">三维向量</param>
        /// <returns>三维向量</returns>
        public static Vector3D FindAnyPerpendicular(this Vector3D v)
        {
            Vector3D u = Vector3D.CrossProduct(new Vector3D(0, 1, 0), v);
            if (u.LengthSquared < 1e-3)
            {
                u = Vector3D.CrossProduct(new Vector3D(1, 0, 0), v);
            }

            return u;
        }

        /// <summary>
        /// 将三维向量转为三维点
        /// </summary>
        /// <param name="n">三维向量</param>
        /// <returns>三维点</returns>
        public static Point3D ToPoint3D(this Vector3D v)
        {
            return new Point3D(v.X, v.Y, v.Z);
        }

        /// <summary>
        /// 将三维点转成三维向量
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public static Vector3D ToVector3D(this Point3D p)
        {
            return new Vector3D(p.X,p.Y,p.Z);
        }

        #endregion
    }
}
